package com.zj.demo

import android.content.Context
import com.xuexiang.xutil.XUtil.runOnUiThread
import com.xuexiang.xutil.tip.ToastUtils.toast
import com.zj.easy.utils.log
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.math.BigDecimal
import java.text.DecimalFormat
import kotlin.math.pow

object Demo2 {

    @JvmStatic
    fun main(args: Array<String>) {
        test3()
    }

    fun test3(){
        var f1=0.3f
        var f2=f1.toBits().toString().toCharArray()
        var sum=0.0.toBigDecimal()
        var j=1
        for (i in 8..31){
            var t=f2[i].toInt()
            if (i==8){
                t=1
            }
            sum=sum+1.0.toBigDecimal().div(2.0.toBigDecimal().pow(j))
            j++
        }
        println("========="+sum)
    }

    fun test1(iv: Context?) {


    }

    private fun test2() {
//        var f1=11112.0
//        var f2=11111.9
//        var f1="1.0".toFloat()
//        var f2="0.9".toFloat()
        var f1: Float = 2599.2F
        var f2: Float = 1110.9F
        var f3 = 123456789f
        var f7 = 0.2999999999999999388

        var f4 = 2.0
        var f6 = 0.3
        var f9 = f7.toBits()
        for (j in 0..100) {
            ("============${Double.fromBits(f9++)}").log()
//            for (i in 0..10) {
//                ("============${ Double.fromBits(f8++)}").log()
//            var f5 = f3 + i*f4 / (2.0.pow(60))
//            ("${f5==f6}======${i}======${f5.toBits()}").log()
//            ("============${1.0.toBigDecimal().divide(2.0.toBigDecimal().pow(i)).toPlainString()}").log()
//            }
        }

        ("============${f1 - f2}").log()
        ("============${(0.3 - 0.2) == 0.1}").log()
        ("============${DecimalFormat.getInstance().format(Int.MAX_VALUE / 100.0F)}").log()
        var bd1 = BigDecimal(f1.toString())
        var bd2 = BigDecimal(f2.toString())
        ("============${(bd1 - bd2).setScale(1)}").log()
        ("============${2.0.pow(-7)}").log()

//        for (i in 0..99) {
//            comput(Int.MAX_VALUE / 10000 * i, Int.MAX_VALUE / 10000 * (i + 1))
//        }

    }

    private var num = Int.MAX_VALUE
    private fun comput(i: Int, i1: Int) {
//        var big=BigDecimal(100)
        GlobalScope.launch(Dispatchers.IO) {
//            var time=System.currentTimeMillis()
            val length = i1.toString().length + 1
            for (i in i..i1) {
                var re = (i / 100.0).toString()
//                if (i / 100.0 != BigDecimal(i).divide(big).toDouble()) {

//                ("=======----=====${i}").log()
                if (re.length > length) {
                    runOnUiThread { toast(("===${i}")) }
                } else {
                    ("===${num}===${re}").log()
                }
                num--
//                ("===${num}").log()
            }
//            ("=======----=====${System.currentTimeMillis()-time}").log()
        }
    }

}